#region Using directives

using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;
using System.Reflection;
using System.IO;

using FighterChessEngine.FC_Board;
using FighterChessSkin.FC_Board;

#endregion

namespace FighterChess
{
    /// <summary>
    /// Summary description for frmOption.
    /// </summary>
    public class frmOption : System.Windows.Forms.Form
    {
        private Button btnContinue;
        private Button btnSave;
        private Button btnNewGame;
        private Button btnQuit;
        public frmMain mainForm;

        public frmOption()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.btnContinue = new System.Windows.Forms.Button();
            this.btnSave = new System.Windows.Forms.Button();
            this.btnQuit = new System.Windows.Forms.Button();
            this.btnNewGame = new System.Windows.Forms.Button();
            // 
            // btnContinue
            // 
            this.btnContinue.Location = new System.Drawing.Point(49, 50);
            this.btnContinue.Size = new System.Drawing.Size(141, 41);
            this.btnContinue.Text = "Continue";
            this.btnContinue.Click += new System.EventHandler(this.btnContinue_Click);
            // 
            // btnSave
            // 
            this.btnSave.Location = new System.Drawing.Point(49, 144);
            this.btnSave.Size = new System.Drawing.Size(141, 41);
            this.btnSave.Text = "Save";
            this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
            // 
            // btnQuit
            // 
            this.btnQuit.Location = new System.Drawing.Point(49, 191);
            this.btnQuit.Size = new System.Drawing.Size(141, 41);
            this.btnQuit.Text = "Quit";
            this.btnQuit.Click += new System.EventHandler(this.btnQuit_Click);
            // 
            // btnNewGame
            // 
            this.btnNewGame.Location = new System.Drawing.Point(49, 97);
            this.btnNewGame.Size = new System.Drawing.Size(141, 41);
            this.btnNewGame.Text = "New game";
            this.btnNewGame.Click += new System.EventHandler(this.btnNewGame_Click);
            // 
            // frmOption
            // 
            this.ClientSize = new System.Drawing.Size(240, 294);
            this.ControlBox = false;
            this.Controls.Add(this.btnQuit);
            this.Controls.Add(this.btnSave);
            this.Controls.Add(this.btnNewGame);
            this.Controls.Add(this.btnContinue);
            this.Text = "Option";
            this.Load += new System.EventHandler(this.frmOption_Load);

        }

        #endregion

        private readonly string appDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
        private readonly string saveDir = "Save";
        private readonly string saveFile = "save.data";

        private void btnQuit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnContinue_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            Continue();
        }

        private void btnNewGame_Click(object sender, EventArgs e)
        {
            if (this.mainForm != null)
            {
                this.mainForm.Close();
                this.mainForm.Dispose();
            }

            frmBeginGame.Instance.Show();
            GC.Collect();
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            /*OpenFileDialog dlg = new OpenFileDialog();
            if (dlg.ShowDialog() == DialogResult.Cancel)
                return;*/

            if (!Directory.Exists(appDir + "\\" + saveDir))
                Directory.CreateDirectory(appDir + "\\" + saveDir);

            string filename = appDir + "\\" + saveDir + "\\" + saveFile;
            this.mainForm.UIBoard.Board.Save(filename);

            Continue();
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            /*SaveFileDialog dlg = new SaveFileDialog();
            if (dlg.ShowDialog() == DialogResult.Cancel)
                return;*/

            string filename = appDir + "\\" + saveDir + "\\" + saveFile;
            Board board = new Board(filename);
            this.mainForm.UIBoard = null;
            GC.Collect();
            this.mainForm.UIBoard = new UIBoard(board);

            Continue();
        }

        private void Continue()
        {
            this.mainForm.Show();
            this.Close();
            this.Dispose();
        }

        private void frmOption_Load(object sender, EventArgs e)
        {
            if (this.mainForm.UIBoard.Board.GameIsEnd)
                btnSave.Enabled = false;
        }
    }
}
